const fileCache = require('think-cache-file');
const nunjucks = require('think-view-nunjucks');
const JWTSession = require('think-session-jwt');
const mysql = require('think-model-mysql');
const {Basic, Console, File, DateFile} = require('think-logger3');
const path = require('path');
const isDev = think.env === 'development';

/**
 * cache adapter config
 */
exports.cache = {
    type: 'file',
    common: {
        timeout: 24 * 60 * 60 * 1000 // millisecond
    },
    file: {
        handle: fileCache,
        cachePath: path.join(think.ROOT_PATH, 'runtime/cache'), // absoulte path is necessarily required
        pathDepth: 1,
        gcInterval: 24 * 60 * 60 * 1000 // gc interval
    }
};

/**
 * MYSQL配置。
 */
exports.model = {
    type: 'mysql',
    common: {
        logConnect: isDev,
        logSql: isDev,
        logger: msg => think.logger.info(msg)
    },
    mysql: {
        handle: mysql,
        database: 'facejoy',
        prefix: '',
        encoding: 'utf8',
        host: 'rm-uf6ndtys7ewm477c9go.mysql.rds.aliyuncs.com',
        port: 3306,
        user: 'facejoy_user',
        password: 'FaceJoy!!!',
        dateStrings: true
    }
};

/**
 * session adapter config
 */
exports.session = {
    type: 'jwt',
    common: {
        cookie: {
            name: 'ThinkJs'
        }
    },
    jwt: {
        handle: JWTSession,
        secret: 'PYmOspMxrmOspmOsu',
        tokenType: 'header',    // ['query', 'body', 'header', 'cookie'], 'cookie' is default
        tokenName: 'jwt',       // if tokenType not 'cookie', this will be token name, 'jwt' is default
        sign: {
            // sign options is not required
        },
        verify: {
            // verify options is not required
        }
    }
};

/**
 * view adapter config
 */
exports.view = {
    type: 'nunjucks',

    common: {
        viewPath: path.join(think.ROOT_PATH, 'view'),
        sep: '_',
        extname: '.html'
    },

    nunjucks: {
        handle: nunjucks
    }
};

/**
 * 日志适配器。
 */
exports.logger = {
    type: isDev ? 'console' : 'advanced',

    console: {
        handle: Console
    },
    advanced: {
        handle: Basic,
        appenders: {
            everything: {
                type: 'file',
                filename: path.join(think.ROOT_PATH, 'logs/all.log')
            },
            emergencies: {
                type: 'file',
                filename: path.join(think.ROOT_PATH, 'logs/just-error.log')
            },
            'just-errors': {
                type: 'logLevelFilter',
                appender: 'emergencies',
                level: 'error'
            }
        },
        categories: {
            default: {
                appenders: ['just-errors', 'everything'],
                level: 'debug'
            }
        }
    }
};
